package com.example.demo.controller;

import com.example.demo.erp.BdCustomer;
import com.example.demo.erp.SeqHelper;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.*;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import lombok.var;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * TODO
 *
 * @author liuningning
 * @date 2024/12/27 14:51
 */
@RestController
@RequestMapping("/erp")
public class ErpController {

    private static final String FName = "aukh_" + UUID.randomUUID();
    private static final String FShortName = "aukh_" + UUID.randomUUID();
    private static final String FNumber = SeqHelper.genNumber("BC");
    @GetMapping("/getCustomer")
    public void getCustomer() throws Exception {
        K3CloudApi api = new K3CloudApi();
        var para = new QueryParam();
        para.setFormId("BD_Customer");
        para.setFieldKeys("FNumber,FName,");
        para.setFilterString("Fnumber=\'"+ FNumber +"\'");
        Gson gson = new Gson();
        List result = api.executeBillQuery(para, BdCustomer.class);
        System.out.println("客户单据查询接口: " + gson.toJson(result));
        System.out.println(api);
    }

    @GetMapping("/saveCustomer")
    public void saveCustomer() throws Exception {
        K3CloudApi api = new K3CloudApi();
        String para = "{\"FName\":  "+"\""+FName+"\""+",\"FNumber\": "+"\""+FNumber+"\""+",\"FCreateOrgId\": {\"FNumber\": \"100\"},\"FUseOrgId\": {\"FNumber\": \"100\"},\"FCOUNTRY\": {    \"FNumber\": \"China\"},\"FINVOICETITLE\": \"zzl\",\"FCustTypeId\": {    \"FNumber\": \"KHLB001_SYS\"},\"FTRADINGCURRID\": {    \"FNumber\": \"PRE001\"},\"FInvoiceType\": \"1\",\"FTaxType\": {    \"FNumber\": \"SFL02_SYS\"},\"FPriority\": 1,\"FTaxRate\": {    \"FNumber\": \"SL02_SYS\"},\"FISCREDITCHECK\": true,\"FIsTrade\": true    }";
        Map<String, Object> map = new HashMap<>();
        map = new Gson().fromJson(para, map.getClass());
        SaveResult sRet = api.save("BD_Customer", new SaveParam<Map>(map));
        var gson = new Gson();
        if (sRet.isSuccessfully()) {
            System.out.printf("客户保存接口: %s%n", gson.toJson(sRet.getResult()));
        } else {
            System.out.println("客户保存接口: " + gson.toJson(sRet.getResult()));
        }
    }

    @GetMapping("/submitCustomer")
    public void submitCustomer() throws Exception {
        K3CloudApi api = new K3CloudApi();
        OperateParam para = new OperateParam();
        List<String> Numbers = new ArrayList<>();
        Numbers.add(FNumber);
        para.setNumbers(Numbers);
        OperatorResult result = api.submit("BD_Customer", para);
        Gson gson = new Gson();
        if (result.getResult().getResponseStatus().isIsSuccess()) {
            System.out.printf("客户提交接口: %s%n", gson.toJson(result.getResult()));
        } else {
            System.out.println("客户提交接口：" + gson.toJson(result.getResult()));
        }
    }

    @GetMapping("/approveCustomer")
    public void approveCustomer() throws Exception {
        K3CloudApi api = new K3CloudApi();
        var para = new OperateParam();
        List<String> Numbers = new ArrayList<>();
        Numbers.add(FNumber);
        para.setNumbers(Numbers);
        OperatorResult result = api.audit("BD_Customer", para );
        Gson gson = new Gson();
        if (result.getResult().getResponseStatus().isIsSuccess()) {
            System.out.printf("客户审核接口: %s%n", gson.toJson(result.getResult()));
        } else {
            System.out.println("客户审核接口: " + gson.toJson(result.getResult().getResponseStatus()));
        }
    }

    @GetMapping("/viewCustomer")
    public void viewCustomer() throws Exception {
        K3CloudApi api = new K3CloudApi();
        var para = new OperateParam();
        para.setNumber(FNumber);
        OperatorResult result = api.view("BD_Customer", para);
        Gson gson = new Gson();
        if (result.getResult().getResponseStatus().isIsSuccess()) {
            System.out.printf("客户查看接口: %s%n", gson.toJson(result.getResult()));
        } else {
            System.out.println("客户查看接口: " + gson.toJson(result.getResult()));
        }
    }

    @GetMapping("/getPurchase")
    public void getPurchase() throws Exception {
        K3CloudApi api = new K3CloudApi();
        String json = "{\n" +
                "    \"FormId\": \"PUR_PurchaseOrder\",\n" +
                "    \"FieldKeys\": \"FID,FBillNo\",\n" +
                "    \"FilterString\": \"FBillNo=\'" + FNumber + "\'\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";

        String result = String.valueOf(api.executeBillQuery(json));
        System.out.println("采购订单单据查询接口: " + result);
    }
}
